package server

import (
	"errors"
	"github.com/google/uuid"
	"log"
	"travel/pkg"
	"travel/rpc/initiate/mysql"
	"travel/rpc/proto/travel"
)

func Register(in *travel.RegisterReq) (*travel.RegisterResp, error) {
	u := mysql.User{
		Name:     in.Name,
		Phone:    in.Phone,
		Password: pkg.Md5Password(in.Password),
	}
	err := mysql.DB.Where("phone =?", u.Phone).Error
	if err != nil {
		log.Println("当前账号已注册请前往登录")
		return nil, err
	}
	err = mysql.DB.Create(&u).Error
	if err != nil {
		log.Println("注册失败")
		return nil, err
	}
	return &travel.RegisterResp{Greet: "注册成功"}, nil
}

func Login(in *travel.LoginReq) (*travel.LoginResp, error) {
	u := mysql.User{
		Name:     in.Name,
		Phone:    in.Phone,
		Password: in.Password,
	}
	err := mysql.DB.Where("phone =?", u.Phone).Find(&u).Error
	if err != nil {
		log.Println("当前登录失败")
		return nil, err
	}
	token, err := pkg.NewJWT("2209a").CreateToken(pkg.CustomClaims{
		ID: u.ID,
	})
	if err != nil {
		log.Println("登录不成功")
		return nil, err
	}
	return &travel.LoginResp{Greet: token}, nil
}

func ScenicSo(in *travel.ScenicSoReq) (*travel.ScenicSoResp, error) {
	s := mysql.Scenic{
		Name: in.Name,
	}
	err := mysql.DB.Where("name =?", in.Name).Find(&s).Error
	if err != nil {
		log.Println("搜索失败")
		return nil, err
	}
	return &travel.ScenicSoResp{Greet: "搜所成功"}, nil
}

func GoodsCreate(in *travel.GoodsCreateReq) (*travel.GoodsCreateResp, error) {
	g := mysql.Good{
		Name:  in.Name,
		Num:   in.Num,
		Dates: in.Dates,
	}
	err := g.Find(in.Name)
	if g.Num < in.Num {
		log.Println("商品库存不够")
		return nil, err
	}
	err = g.Updated(in.Num)
	return &travel.GoodsCreateResp{Greet: "商品购买成功"}, nil
}

func RealName(in *travel.RealNameReq) (*travel.RealNameResp, error) {
	r := mysql.RealName{
		UserId:  in.UserId,
		ReaInes: in.ReaInes,
		IdCard:  in.IdCard,
	}
	err := mysql.DB.Where("id_card =?", r.IdCard).Find(&r).Error
	if err != nil {
		log.Println(":未查询到该用户")
		return nil, errors.New("未查询到该用户")
	}
	if r.ID != 0 {
		log.Println("未找到该用户")
		return nil, errors.New("未找到该用户")
	}
	if !pkg.RealName(in.IdCard, in.ReaInes) {
		log.Println("当前账号已经被实名或实名已经被使用")
		return nil, errors.New("当前账号已经被实名或实名已经被使用")
	}
	err = mysql.DB.Create(&r).Error
	if err != nil {
		log.Println("添加实名失败")
		return nil, errors.New("添加实名失败")
	}
	return &travel.RealNameResp{Greet: "实名成功"}, nil
}

func OrderSn(in *travel.OrderSnReq) (*travel.OrderSnResp, error) {
	o := mysql.Order{}
	err := mysql.DB.Where("goods_id =?", o.ID).Find(&o).Error
	if err != nil {
		log.Println("未查询到该商品")
		return nil, err
	}
	ordersn := uuid.New().String()
	or := mysql.Order{
		UserId:  in.UserId,
		GoodsId: in.GoodsId,
		OrderSn: ordersn,
		Name:    o.Name,
		Price:   o.Price,
		Status:  o.Status,
	}
	err = mysql.DB.Create(&or).Error
	if err != nil {
		log.Println("下单失败")
		return nil, err
	}

	return &travel.OrderSnResp{
		Name:   o.Name,
		PayURL: o.OrderSn,
	}, nil
}

func Insure(in *travel.InsureReq) (*travel.InsureResp, error) {
	user := mysql.OrderUser{
		UserId: int32(in.Userid),
		Name:   in.Name,
		Price:  float64(in.Price),
	}
	err := mysql.DB.Create(&user).Error
	if err != nil {
		log.Println("投保失败")
		return nil, err
	}
	return &travel.InsureResp{Success: true}, nil
}

func Callback(in *travel.CallbackReq) (*travel.CallbackResp, error) {
	var statusec string
	if in.Status == 1 {
		statusec = "TRADE_FINISHED"
	} else if in.Status == 2 {
		statusec = "TRADE_SUCCESS"
	} else if in.Status == 3 {
		statusec = "WAIT_BUYER_PAY"
	} else if in.Status == 4 {
		statusec = "TRADE_CLOSED"
	}
	err := mysql.DB.Where("status=?", in.Status).Update("statusec", statusec).Error
	if err != nil {
		log.Println("状态回调修改失败")
		return nil, err
	}
	return &travel.CallbackResp{Greet: "状态回调修改成功"}, nil
}

func OrderFind(in *travel.OrderFindReq) (*travel.OrderFindResp, error) {
	order := mysql.Order{}
	err := mysql.DB.Where("order_id =?", in.OrderId).Find(&order).Error
	if err != nil {
		log.Println("展示失败")
		return nil, err
	}
	return &travel.OrderFindResp{Greet: "展示成功"}, nil
}

func Refund(in *travel.RefundReq) (*travel.RefundResp, error) {
	order := mysql.Order{}
	err := mysql.DB.Where("status =?", in.Status).Find(&order).Error
	if err != nil {
		log.Println("未找到次订单号")
	}
	if in.Status == 0 {
		order.Del(in.Goodsid)
		log.Println("退单成功")
	}
	return &travel.RefundResp{Success: true}, nil
}
